package com.kimho.rollback.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kimho.inhouse.domain.Inhouse;
import com.kimho.inhouse.persistence.InhouseMapper;
import com.kimho.product.persistence.ProductMapper;
import com.kimho.rollback.domain.Rollback;
import com.kimho.rollback.psersistence.RollbackMapper;
import com.kimho.util.Util;
@Service
public class RollbackService {
	@Autowired
	private RollbackMapper rollbackMapper;
	@Autowired
	private ProductMapper productMapper;
	@Autowired
	private InhouseMapper InhouseMapper;
	@Transactional
	public void returnback( Rollback rollback)
	{
		Inhouse inhouse=new Inhouse();
		String from_addr;
		String to_addr;
		
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("object_id",rollback.getObject_id());
		System.out.println(rollback.getObject_id());
		inhouse = InhouseMapper.getInhouse(param);
		String newid = Util.getUUID();
	    inhouse.setObject_id(newid);
		from_addr=inhouse.getTo_addr();
		to_addr=inhouse.getFrom_addr();
		
		inhouse.setFrom_addr(from_addr);
		inhouse.setTo_addr(to_addr);
		inhouse.setObj_num(String.valueOf(Integer.parseInt( inhouse.getObj_num())-Integer.parseInt(rollback.getObj_num())));
		inhouse.setTraff_fee(rollback.getTraff_fee());
		inhouse.setSaf_fee(rollback.getSaf_fee());
		inhouse.setOver("1");//设置物件状态为1,表示货物入库
		InhouseMapper.insertInhouse(inhouse);
	}
	public void updateRollback(Rollback rollback) {
		//新增物流
		
	/*更新COST表状态*/
		
		String ct_type=rollbackMapper.getCTidbyCType(rollback);

		if( null != ct_type)
		{
			rollback.setCost_type(ct_type);
		}
		rollbackMapper.updateCost(rollback);
		/*更新货号状态为走完流程*/
		rollbackMapper.updateObjectdet(rollback);
		/*将费用插入到代收表中*/
//		if("1".equals(rollback.getSettle())){
		/*如果有代收费用，插入到代收表中。*/
		if(rollback.getT_inst_fee()>0)
		{
				rollbackMapper.insertInst(rollback);//不论是否结清，插入到代收表
		}
		//	}
		/*如果有中转货物，插入到中转表*/
		if(null!=rollback.getTransitf())
		{
			rollbackMapper.insertTransitdet(rollback);
		}
	/*	Rollback rollback_t = rollbackMapper.getdealidbyobjid(rollback);
		rollback.setDeal_id(rollback_t.getDeal_id());
		List<String> logistics_id =this.productMapper.getCountLogisObjet(rollback.getObject_id());
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("object_id", rollback.getObject_id());
		if(logistics_id.size()==1){//判断物件对应的流水单一共对应多少货物,如果只有一个则要删除流水单主表记录
			map.remove("object_id");
			map.put("logistics_id", logistics_id.get(0));
			this.productMapper.deletelogistics(map);
		};
		rollbackMapper.insertTransitdet(rollback);
	
		this.productMapper.deletelogobj(map);
	*/
	}
	public List<Rollback> getRollbackList(Map<String, Object> param) {
		return rollbackMapper.getRollbackList(param);
	}
	public Long getTotal(Map<String, Object> param){
		return rollbackMapper.getTotal(param);
	}
}
